import dayjs from "dayjs";

const DATE_TIME_FORMAT = "YY-MM-DD HH:mm";
const DATE_FORMAT = "YYYY-MM-DD";

export function formatToDateTime(date, format = DATE_TIME_FORMAT) {
  return dayjs(date).format(format);
}

export function formatToDate(date, format = DATE_FORMAT) {
  return dayjs(date).format(format);
}

export function formatFileSize(bytes, decimals = 2) {
  if (typeof bytes !== "number" || isNaN(bytes)) return "--";
  if (bytes === 0) return "0 Bytes";

  const k = 1024;
  const dm = decimals < 0 ? 0 : decimals;
  const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB"];

  const i = Math.floor(Math.log(bytes) / Math.log(k));

  if (i === 0) return bytes + " " + sizes[i];

  return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}
